<?php

namespace app\models;

use Yii;

class KeywordModel
{
    public function getAllKeyword(){
        return \Yii::$app->db->createCommand("select * from p_keyword")->queryAll();
    }

    public function findKeywordByName($keyword){
        $fileds="reply";
        $result= \Yii::$app->db->createCommand("select $fileds from p_keyword where keyword=:keyword order by rand() limit 1",[":keyword"=>$keyword])->queryScalar();
        if(empty($result)){
            $result= \Yii::$app->db->createCommand("select $fileds from p_keyword where keyword like :keyword order by rand() limit 1",[":keyword"=>"%{$keyword}%"])->queryScalar();
        }
        return $result;
    }

    public function findKeywordById($keyword_id){
        $fileds="*";
        return \Yii::$app->db->createCommand("select $fileds from p_keyword where id=:keyword_id",[":keyword_id"=>$keyword_id])->queryOne();
    }

    public function insertKeyword($data){
        \Yii::$app->db->createCommand()->insert("p_keyword",$data)->execute();
        return \Yii::$app->db->getLastInsertId();
    }

    public function updateKeyword($data,$keyword_id){
        return \Yii::$app->db->createCommand()->update("p_keyword",$data,["id"=>$keyword_id])->execute();
    }

    public function deleteKeyword($keyword_id){
        return \Yii::$app->db->createCommand()->delete("p_keyword",["id"=>$keyword_id])->execute();
    }

    public function batchInsertKeyword($column_list, $datalist)
    {
        if (!empty($datalist)) {
            $this->db->createCommand()->batchInsert("p_keyword", $column_list, $datalist)->execute();
        }
    }
}